package com.bestom.waterquality.logic;

import android.content.Context;
import android.view.View;

import com.bestom.waterquality.R;
import com.bestom.waterquality.WaterQualityApplication;
import com.bestom.waterquality.entity.Constant;
import com.bestom.waterquality.entity.NodeBean;
import com.bestom.waterquality.services.MQTTService;
import com.bestom.waterquality.services.MQTTService2;
import com.bestom.waterquality.util.CommandExec;
import com.suke.widget.SwitchButton;

/**
 * 继电器设置
 * */
public class RelaySet {

    private Context mContext;

    private RelaySet() {
    }

    private static RelaySet _instance = null;

    public static RelaySet getInstance() {
        if (_instance == null)
            _instance = new RelaySet();
        return _instance;
    }

    private SwitchButton relaySb1;
    private SwitchButton relaySb2;
    private SwitchButton relaySb3;
    private SwitchButton relaySb4;
    public void setDataAndListen(Context context, View view){

        if (mContext == null){

            mContext = context;
            //继电器1
            relaySb1 = view.findViewById(R.id.relay_1_sb);
            relaySb1.setChecked(CommandExec.execCommand(Constant.CAT_GP122,false).successMsg.equals("1"));
            relaySb1.setOnCheckedChangeListener((view1, isChecked) -> CommandExec.execCommand(isChecked ? Constant.OPEN_GP122 : Constant.CLOSE_GP122, false));
            //继电器2
            relaySb2 = view.findViewById(R.id.relay_2_sb);
            relaySb2.setChecked(CommandExec.execCommand(Constant.CAT_GP123,false).successMsg.equals("1"));
            relaySb2.setOnCheckedChangeListener((view12, isChecked) -> CommandExec.execCommand(isChecked ? Constant.OPEN_GP123 : Constant.CLOSE_GP123,false));
            //继电器3
            relaySb3 = view.findViewById(R.id.relay_3_sb);
            relaySb3.setChecked(CommandExec.execCommand(Constant.CAT_GP120,false).successMsg.equals("1"));
            relaySb3.setOnCheckedChangeListener((view13, isChecked) -> CommandExec.execCommand(isChecked ? Constant.OPEN_GP120 : Constant.CLOSE_GP120,false));
            //继电器4
            relaySb4 = view.findViewById(R.id.relay_4_sb);
            relaySb4.setChecked(CommandExec.execCommand(Constant.CAT_GP121,false).successMsg.equals("1"));
            relaySb4.setOnCheckedChangeListener((view14, isChecked) -> CommandExec.execCommand(isChecked ? Constant.OPEN_GP121 : Constant.CLOSE_GP121,false));
        }
    }

    /**为 MQTT 下发 控制继电器 */
    public void setValue(String relayId, String setValue){
        if (relayId.isEmpty()) return;
        if (setValue.isEmpty()) return;

        int i = 0;
        String[] split = setValue.split(",");
        StringBuilder status = new StringBuilder();
        for (String id : relayId.split(",")){
            switch (id){
                case "1":
                    if (relaySb1 != null) relaySb1.setChecked(split[i].equals("1"));
                    else CommandExec.execCommand(split[i].equals("1") ? Constant.OPEN_GP122 : Constant.CLOSE_GP122, false);
                    break;
                case "2":
                    if (relaySb2 != null) relaySb2.setChecked(split[i].equals("1"));
                    else CommandExec.execCommand(split[i].equals("1") ? Constant.OPEN_GP123 : Constant.CLOSE_GP123, false);
                    break;
                case "3":
                    if (relaySb3 != null) relaySb3.setChecked(split[i].equals("1"));
                    else CommandExec.execCommand(split[i].equals("1") ? Constant.OPEN_GP120 : Constant.CLOSE_GP120, false);
                    break;
                case "4":
                    if (relaySb4 != null) relaySb4.setChecked(split[i].equals("1"));
                    else CommandExec.execCommand(split[i].equals("1") ? Constant.OPEN_GP121 : Constant.CLOSE_GP121, false);
                    break;
            }
            i+=1;
            if (i == split.length) status.append("0");
            else status.append("0,");
        }
        // 响应MQTT控制结果，针对该板子，此控制方式结果必成功，因此不做控制失败判断
        if (WaterQualityApplication.mtIsOk){
            MQTTService.publish(Constant.RelayPublishTopic,new NodeBean(Constant.defDeviceId, status.toString(),relayId,setValue,"success"));
        }
        if (WaterQualityApplication.mt2IsOk){
            MQTTService2.publish(Constant.RelayPublishTopic,new NodeBean(Constant.defDeviceId, status.toString(),relayId,setValue,"success"));
        }
    }
}
